package com.flame.auth.client.config;

import com.flame.auth.client.interceptor.AuthInterceptor;
import com.flame.auth.client.interceptor.TokenInterceptor;
import com.flame.auth.client.mybatis.DataAuthInterceptor;
import com.flame.auth.core.config.AuthConfigurationProperties;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author wuxintong
 * @since 2022/12/14
 */
@ComponentScan("com.flame.auth.client")
@EnableConfigurationProperties(AuthClientConfigurationProperties.class)
@Configuration
public class AuthClientConfiguration implements WebMvcConfigurer {

    @Autowired
    private AuthConfigurationProperties authProperties;

    @Autowired
    private AuthClientConfigurationProperties authClientConfigurationProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new TokenInterceptor(authProperties, authClientConfigurationProperties)).order(10)
            .addPathPatterns("/**");
        registry.addInterceptor(new AuthInterceptor()).order(20).addPathPatterns("/**");
    }

    @ConditionalOnClass({SqlSessionFactory.class})
    @Bean
    @Order(Integer.MAX_VALUE - 5)
    public DataAuthInterceptor dataAuthInterceptor() {
        return new DataAuthInterceptor();
    }
}
